433B - Kuriyama Mirai's Stones - CodeForces Solution


dp implementation sortings *1200

Please click on ads to support us..

Python Code:

n=int(input())

costs=list(map(int,input().split()))
cum=[costs[0]]
for i in range(1,n):
    cum.append(costs[i]+cum[i-1])

    
costs.sort()
cumsort=[costs[0]]
for i in range(1,n):
    cumsort.append(costs[i]+cumsort[i-1])
    
    
m=int(input())
arr=[]
for i in range(m):
    arr.append(list(map(int,input().split())))


for i in arr:
    a=i[0]
    b=i[1]-1
    c=i[2]-1
    if a==1:
        if b==0 : print(cum[c])
        else: print(cum[c]-cum[b-1])
    else:
        if b==0:print(cumsort[c])
        else:print(cumsort[c]-cumsort[b-1])
 

       

C++ Code:

#include <iostream>
#include <algorithm>

using namespace std;

long long a[100001], a2[100001];

void f1(int l, int r) {
    cout << a[r] - a[l - 1] << endl;
}

void f2(int l, int r) {
    cout << a2[r] - a2[l - 1] << endl;
}

int main()
{
    int t, n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        a[i]= a2[i] = x;
    }
    sort(a2 + 1, a2 + n + 1);
    for (int i = 1; i <= n; i++) {
        a[i] += a[i - 1];
        a2[i] += a2[i - 1];
    }
    cin >> t;
    for (int i = 1; i <= t; i++) {
        int type, l, r;
        cin >> type >> l >> r;
        if (type == 1)
            f1(l, r);
        else f2(l, r);
    }
}


Comments

Submit
0 Comments
More Questions

1141B - Maximal Continuous Rest
1341A - Nastya and Rice
1133A - Middle of the Contest
385A - Bear and Raspberry
1311B - WeirdSort
1713F - Lost Array
236B - Easy Number Challenge
275A - Lights Out
147A - Punctuation
253A - Boys and Girls
1327E - Count The Blocks
984A - Game
12B - Correct Solution
1355B - Young Explorers
485A - Factory
628A - Tennis Tournament
1436B - Prime Square
1707B - Difference Array
1422C - Bargain
1611F - ATM and Students
660A - Co-prime Array
1692F - 3SUM
1470A - Strange Birthday Party
190D - Non-Secret Cypher
1721B - Deadly Laser
1721C - Min-Max Array Transformation
1721A - Image
1180C - Valeriy and Deque
557A - Ilya and Diplomas
1037D - Valid BFS